package com.imooc.business.core.executor.query.base.table;


import com.google.common.collect.Maps;
import com.imooc.business.core.executor.query.base.AbstractTableService;
import com.imooc.module.query.enums.OrganType;
import com.imooc.module.query.enums.TableType;
import com.imooc.module.query.request.QueryRequest;
import com.imooc.module.query.request.SupportType;
import com.imooc.module.query.response.QueryDataResponse;
import com.imooc.user.provider.model.GroupType;
import com.imooc.user.provider.model.UserCenterGroupVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tim
 * @description: 学校报表
 */
@Service
public class SchoolTableService extends AbstractTableService<UserCenterGroupVo,Long> {

    /**
     * 查询学校下面群组数据
     * @param request
     */
    @Override
    protected List<UserCenterGroupVo> getOrganList(QueryRequest request) {
        UserCenterGroupVo group = request.getGroup();
        List<UserCenterGroupVo> schools = groupService.getGroupBySchoolId(group.getParentId(), Arrays.asList(GroupType.SCHOOL.getType()));
        return schools;
    }

    @Override
    protected Map<Object, Map<String, Object>> getDataMap(QueryRequest request, List<UserCenterGroupVo> schools) {
        if (CollectionUtils.isNotEmpty(schools)) {
            request.setSchoolIds(schools.stream().map(s -> s.getId()).distinct().collect(Collectors.toList()));

            Map<Object, Map<String, Object>> schoolMap = execQuery(request);
            return schoolMap;
        }
        return Maps.newHashMap();
    }

    @Override
    protected void setSumaryAndClickAble(QueryDataResponse.TableData tableData, UserCenterGroupVo school) {
        tableData.setIsSummary(false);
        tableData.setClickAble(true);
    }

    @Override
    protected String getName(UserCenterGroupVo school) {
        return school.getGroupName();
    }

    @Override
    protected Long getOrganId(UserCenterGroupVo school) {
        return school.getId();
    }

    @Override
    public Boolean supportType(SupportType supportType) {
        return OrganType.SCHOOL.equals(supportType.getOrganType()) &&
                TableType.TABLE.equals(supportType.getTableType());
    }
}
